﻿using System;
using System.Data;
using System.Web;
using System.Collections;

using Wicresoft.Framework.Organization;
using System.Configuration;

namespace Wicresoft.Busi.BizLogic
{
    public class BasePage
    {
        public BasePage()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private static int _deep = int.MinValue;
        public static int Deep
        {
            get
            {
                try
                {
                    if (_deep == int.MinValue)
                        _deep = int.Parse(ConfigurationManager.AppSettings["Dept.Deep"].ToString());
                    return _deep;
                }
                catch
                {
                    return 2;
                }
            }
        }

        //public static Department GetDept(Guid UserID)
        //{
        //    User user = OrganizationHelper.GetUserInfo(UserID);
        //    Department dept = OrganizationHelper.GetDepartmentInfo(user.MainDepartmentID);
        //    int pathDeep = (dept.IDPath.Split('/')).Length;
        //    if (pathDeep - 1 <= BasePage.Deep)
        //        return dept;
        //    else
        //        return GetParentDept(dept);
        //}

        //private static Department GetParentDept(Department dept)
        //{
        //    Department parentDept = OrganizationHelper.GetDepartmentInfo(dept.MainDepartmentID);
        //    int parentPathDeep = (parentDept.IDPath.Split('/')).Length;
        //    if (parentPathDeep - 1 <= BasePage.Deep)
        //        return parentDept;
        //    else
        //        return GetParentDept(parentDept);
        //}

        public static Department GetDept(Guid userID)
        {
          //  User user = OrganizationHelper.GetUserInfo(userID);
            Department dept = Wicresoft.Framework.Authentication.AuthenticationContext.CurrentUser.GetMainDepartment();
            string[] IDPaths = dept.IDPath.Split(new char[] { '/' });
            if (IDPaths.Length > BasePage.Deep)
                return OrganizationHelper.GetDepartmentInfo(new Guid(IDPaths[BasePage.Deep]));
            else
                return dept;
        }

        private static Department GetParentDept(Guid userID)
        {
            User user = OrganizationHelper.GetUserInfo(userID);
            Department dept = OrganizationHelper.GetDepartmentInfo(user.MainDepartmentID);
            string[] IDPaths = dept.IDPath.Split(new char[] { '/' });
            if (IDPaths.Length > BasePage.Deep)
                return OrganizationHelper.GetDepartmentInfo(new Guid(IDPaths[BasePage.Deep]));
            else
                return dept;
        }

    }
}

